package com.le.t1.jie.imp;

import com.le.t1.jie.personj;
import com.le.t1.person;
import com.le.t1.工具.JDBC;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class personjimp implements personj {

    private static final String SQL_INSERT = "insert into person (name,password) values(?,?)";


    private static final String SQL_SELECT = "select * from person  ";
    Connection connection=null;
    PreparedStatement pre=null;
    ResultSet rs=null;

    public int insert(person person) throws Exception{

        return update(SQL_INSERT, new Object[] { person.getName(), person.getPassword(), });
    }



    public List<person> select() throws Exception{
        List<person> PersonList = new ArrayList<>();
        connection= JDBC.getConnection();
        pre=connection.prepareStatement(SQL_SELECT);
        rs=pre.executeQuery();
        while (rs.next()){
            person person=new person();
            person.setName(rs.getString(1));
            person.setPassword(rs.getString(2));
          //  System.out.println(person.toString());
            PersonList.add(person);
        }
        JDBC.close(connection,pre,rs);
        return PersonList;
    }

    private int update(String sql, Object[] objects) {
        String name=null;
        String password=null;
        int n=0;
        try {
            connection=JDBC.getConnection();
            pre=connection.prepareStatement(sql);


                name= (String) objects[0];
                password= (String) objects[1];
                pre.setString(1, name);
                pre.setString(2, password);

            n=pre.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        }
        JDBC.close(connection,pre);
        return n;
    }

    public static void main(String[] args) throws Exception {
        person person=new person("567","567");
        personj personj=new personjimp();


        List<person> list=new ArrayList<>();
        list=personj.select();
        for (person person1:list){
            System.out.println(person1.toString());
        }
    }
}
